﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Services;
using MusicStore.WebService.Helpers;

namespace MusicStore.WebService
{
	/// <summary>
	/// Summary description for $codebehindclassname$
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	public class FileReceiver : IHttpHandler
	{

		public void ProcessRequest(HttpContext context)
		{
			string id = context.Request.QueryString["id"];
			using (FileStream fs = File.Create(ImageHelper.BuildImagePath(context, Convert.ToInt32(id))))
			{
				SaveFile(context.Request.InputStream, fs);
			}
		}
		private static void SaveFile(Stream stream, Stream fs)
		{
			byte[] buffer = new byte[4096];
			int bytesRead;
			while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
			{
				fs.Write(buffer, 0, bytesRead);
			}
		}

		public bool IsReusable
		{
			get
			{
				return false;
			}
		}
	}
}
